--[[
    desc: 新版风暴峡湾boss下期预览
    author:Hoo
    time:2022-03-07 14:57:16
]]

---@class UINextBossPreviewNew
local UINextBossPreviewNew = UIBase.New(PanelResNames.UINextBossPreviewNew)
local M = UINextBossPreviewNew

function M.Init()
    UIBase.Init(M)
end

---@protected
function M:OnCreate()
    self:RegisterOnClick(self:GetChild("closeTipsBtn"),   self.onClickCloseSkillTip, self)
    self:RegisterOnClick(self:GetChild("BgClose"),        self.onClickClose, self)
    self:RegisterOnClick(self:GetChild("Button_Confirm"), self.onClickClose, self)

    -- boss信息
    self.txtBossName = self:GetChild("NextBossName")
    self.txtBossDesc = self:GetChild("NextBossTips")
    self.txtTimeDown = self:GetChild("NextBossTime")
    

    -- 技能信息
    self.txtSkillName  = self:GetChild("BossSkillName")
    self.txtSkillDesc  = self:GetChild("BossSkillTips")
    self.txtSkillType  = self:GetChild("SkillTypeTxt")
    self.ctrlSkillTips = self:GetController("tipsShow")
    self.listSkill     = self:GetChild("NextBossSkill")
    self:SetDynamicList(self.listSkill, self.skillItemRenderer, self, true)

    -- 动画模型ui
    self.uiBossModel = self:GetChild("BossModelPos")
end
---@protected
function M:OnEnable()
    self:refreshData()
end
---@protected
function M:OnDisable()
end
---@protected
function M:OnDestroy()
    self.skillDatas = nil
    self.trialsInfo = nil
    self:destroyBossModel()
    self:removeTimer()
end

--- 试炼详情
---@private
function M:refreshData()
    ---@type ResTrialsInfoMessage
    local trialsInfo = logicMgr.TrialsManager.GetTrialsInfo()
    if trialsInfo.trialsId == 0 then return end
    self.trialsInfo = trialsInfo

    -- 试炼配置
    local cfg = logicMgr.TrialsManager.GetTrialsConfig()[tostring(trialsInfo.nextTrialsId)]
    self.bossId   = cfg.f_TrialsBossId
    self.bossDesc = cfg.f_TrialsBossIntroduce or ''

    --- boss技能
    ---@type number[]
    self.skillDatas = {}
    local skillId, count = nil, 1
    local cfgHero = dataTable.getDataTableByStringId("t_hero", tostring(cfg.f_TrialsBossId))
    if #self.skillDatas <= 0 then
        while true do
            count   = count + 1
            skillId = tonumber(cfgHero["f_HeroSkill" .. count])
            if skillId == nil or skillId == 0 then
                break
            end
            table.insert(self.skillDatas, skillId)
        end
    end

    --- 试炼剩余时间
    ---@type number
    self.leftTime = trialsInfo.endTime - HelperFunc.GetServerTime()

    --- 加载ui
    self:loadBossInfo()
    self:loadTimer()
    self:refreshTimeDown()
end

--- boss信息
---@private
function M:loadBossInfo()
    local bossId  = self.bossId
    local cfgBoss = logicMgr.HeroManager.GetHeroConfigbyModelId(bossId)
    if not cfgBoss then return end

    -- 基础信息
    self.txtBossDesc.text = getLanguage(self.bossDesc)
    self.txtBossName.text = getLanguage(cfgBoss.f_HeroName)

    -- 技能
    self.listSkill.numItems = #self.skillDatas

    -- 模型
    local url = logicMgr.HeroManager.GetHeroDrawingRolePath(cfgBoss.f_HeroID)
    resMgr:LoadModelPrefab(url, function(go)
        local drawWrapper = self.drawWrapper
        if (drawWrapper == nil) then
            drawWrapper = GoWrapper.New()
        end
        if (self.bossBody ~= nil) then
            destroyImmediate(self.bossBody)
            self.bossBody = nil
        end

        self.bossBody = newObject(go)
        self.bossBody:SetActive(true)
        self.bossBody:AddComponent(TCanvas)
        self.bossBody:AddComponent(TChangeSortingOrder)

        local aniObj = self.bossBody.transform:Find("Animation").gameObject
        Util.PlayAction(aniObj, "fidle", true, 4)
        logicMgr.EquipManager.setWeaponSkin(aniObj, bossId, false)

        drawWrapper:SetWrapTarget(self.bossBody, false)
        Util.Lua_SetLocalPositionXYZ(self.bossBody.transform,0,0,0)
        Util.Lua_GetLocalPositionXYZ(aniObj.transform,0,0,0)
        Util.Lua_SetLocalScaleXYZ(self.bossBody.transform,240, 240,1)
        Util.Lua_SetLocalRotationXYZ(aniObj.transform, 0, 0, 0)
        self.uiBossModel:SetNativeObject(drawWrapper)
    end)
end
function M:destroyBossModel()
    self.drawWrapper = nil
    if (self.bossBody ~= nil) then
        destroyImmediate(self.bossBody)
        self.bossBody = nil
    end
end

--- boss技能
---@private
---@param index number  索引
---@param obj   GObject 技能item对象
function M:skillItemRenderer(index, obj)
    local skillId   = self.skillDatas[index]
    local skillData = dataTable.getDataTableByStringId("t_skillinfo", tostring(skillId))
    if not skillData then return end

    local icon = obj:GetChild("icon"):GetChild("icon")
    icon.url = EnumConst.AtlasPath.SkillAtlas .. skillData.f_SkillIcon
    
    obj.onClick:Clear()
    obj.onClick:Add(function() self:onClickSkillIcon(skillData) end, self)
end
--- 点击boss技能
---@private
---@param skillData {f_SkillName: string, f_Desc: string} 技能数据
function M:onClickSkillIcon(skillData)
    if not skillData then return end

    self.ctrlSkillTips.selectedIndex = 1
    self.txtSkillName.text = getLanguage(skillData.f_SkillName)
    self.txtSkillDesc.text = getLanguage(skillData.f_Desc)
    self.txtSkillType.text = ""
end

--- 倒计时
---@private
function M:loadTimer()
    if self.timer ~= nil then
        self.timer:Reset(self.onTimer, 1, -1, false)
    else
        self.timer = Timer.New(self.onTimer, 1, -1, false)
    end
    self.timer:Start()
end
function M:onTimer()
    local self = M

    if self.trialsInfo and self.trialsInfo.endTime then
        self.leftTime = math.max(0, self.trialsInfo.endTime - HelperFunc.GetServerTime())
    else
        self.leftTime = math.max(0, self.leftTime - 1)
    end
    -- self.leftTime = math.max(0, self.leftTime - 1)
    self:refreshTimeDown()

    if self.leftTime <= 0 then
        self.timer:Stop()
    end
end
function M:removeTimer()
    if self.timer == nil then return end

    self.timer:Stop()
    self.timer = nil
end
function M:refreshTimeDown()
    if not self.txtTimeDown then return end
    self.txtTimeDown.text = timeToTimeFormat(self.leftTime)
end

--- 关闭技能详情页面
---@private
function M:onClickCloseSkillTip()
    self.ctrlSkillTips.selectedIndex = 0
end

--- 关闭界面
---@private
function M:onClickClose()
    closeUI(PanelResNames.UINextBossPreviewNew)
end

return M
